package com.hyjx.framework.component.propertyconfig;

import com.hyjx.framework.common.util.StringHelper;
import com.hyjx.framework.service.ConfigManager;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.net.URLEncoder;

/**
 * 属性配置控制器
 */
@Controller
@RequestMapping("propertyconfig")
public class PropertyConfigController {

    @Resource
    PropertyConfigService propertyConfigService;

    /**
     * 列表页面
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/gotoList", method = RequestMethod.GET)
    public String gotoList(HttpServletRequest request) {
        return "framework/propertyconfig/list";
    }

    /**
     * 查询列表数据
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "queryList", produces = "application/json; charset=utf-8", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public String queryList(PropertyConfigModel model) {
        return propertyConfigService.queryList(model);
    }

    /**
     * 到查看页面
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "gotoView", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public ModelAndView gotoView(HttpServletRequest request) {
        String id = request.getParameter("id");
        PropertyConfigModel model = (PropertyConfigModel) propertyConfigService.getInfoById(id);
        ModelAndView mav = new ModelAndView("framework/propertyconfig/view");
        mav.addObject("model", model);
        return mav;
    }

    /**
     * 到编辑页面
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "gotoEdit", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public ModelAndView gotoEdit(HttpServletRequest request) {
        try {
            String id = request.getParameter("id");
            ModelAndView mav = new ModelAndView("framework/propertyconfig/add");
            PropertyConfigModel model = (PropertyConfigModel) propertyConfigService.getInfoById(id);
            String keyvalue = model.getProperty_value();
            if (keyvalue != null) {
                keyvalue = keyvalue.replaceAll("\"", "@");
                keyvalue = keyvalue.replaceAll("\\\\", "\\\\\\\\");

                model.setProperty_value(keyvalue);
                keyvalue = URLEncoder.encode(keyvalue);
            }

            request.setAttribute("keyvalue", keyvalue);
            request.setAttribute("model", model);
            mav.addObject("model", model);
            return mav;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 执行保存
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "doEditPropertyConfig", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public String doEdit(PropertyConfigModel model) {
        model.setProperty_key(model.getProperty_key().replaceAll("'", ""));
        if (propertyConfigService.edit(model) > 0) {
            ConfigManager.init();
            return "success";
        } else {
            return "failure";
        }
    }

    @RequestMapping(value = "doDelete", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public String gotoDelete(HttpServletRequest request) {
        PropertyConfigModel model = new PropertyConfigModel();
        //model.setSys_right_config_id(request.getParameter("id"));
        model.setProperty_id(request.getParameter("id"));
        if (propertyConfigService.delete(model) > 0) {
            ConfigManager.init();
            return "success";
        } else {
            return "failure";
        }
    }

    /**
     * 到新增
     *
     * @return
     */
    @RequestMapping(value = "gotoAdd", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public ModelAndView gotoAdd() {
        ModelAndView mav = new ModelAndView("framework/propertyconfig/add");
        mav.addObject("model", new PropertyConfigModel());
        return mav;
    }

    /**
     * 执行新增
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(value = "doAddPropertyConfig", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public String doAdd(HttpServletRequest request, PropertyConfigModel model) {
        PropertyConfigModel modelNew = (PropertyConfigModel) propertyConfigService.getInfoByKey(model.getProperty_key());
        if (modelNew != null) {
            return "repeat";
        }
        model.setProperty_id(StringHelper.getPrimaryKey());
        model.setProperty_key(model.getProperty_key().replaceAll("'", ""));
        if (propertyConfigService.add(model)) {
            ConfigManager.init();
            return "success";
        } else {
            return "failure";
        }
    }

}
